package com.ruoyi.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class DateUtil
{
  private static final Logger logger = Logger.getLogger(DateUtil.class.getName());
  private static final SimpleDateFormat SHOW_DATE = new SimpleDateFormat("yyyy��MM��dd�� HH:mm");
  private static final SimpleDateFormat TIGHT_FORM_DATE = new SimpleDateFormat("yyyyMMddHHmmss");
  private static final SimpleDateFormat SIMPLE_FORM_DATE = new SimpleDateFormat("yyyy-MM-dd");
  private static final SimpleDateFormat SIMPLE_FORM_DATE2 = new SimpleDateFormat("yyyyMMdd");
  private static final String FORMATTYPE1 = "yyyy-MM-dd HH:mm:ss";
  private static final String FORMATTYPE2 = "yyyy/MM/dd HH:mm:ss";
  private static final String FORMATTYPE3 = "yyyy��MM��dd�� HH:mm:ss";
  private static final String FORMATTYPE4 = "yyyy��MM��dd�� HH��mm��ss��";
  private static final String FORMATTYPE5 = "yyyy��MM��dd�� E HH:mm:ss";
  private static final String FORMATTYPE6 = "yyyy��MM��dd�� E HH��mm��ss��";
  private static final String FORMATTYPE7 = "yyyyMMdd";
  private static final String FORMATTYPE8 = "yyyyMMddHHmmss";
  private static final String FORMATTYPE9 = "yyyy-MM-dd";
  private static final String FORMATTYPE10 = "yyyy_MM";
  private static final String FORMATTYPE11 = "yyyy";
  private static final String FORMATTYPE12 = "yyyyMM";
  private static final String FORMATTYPE13 = "yyyy-MM";
  private static final String FORMATTYPE14 = "yyyy��MM��dd�� HH:mm";
  
  public static java.util.Date str2date(String dateStr, String pattern)
  {
    if ((StringUtil.isEmpty(dateStr)) || (StringUtil.isEmpty(pattern))) {
      return null;
    }
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat(pattern);
      return sdf.parse(dateStr);
    }
    catch (Exception ex)
    {
      logger.warning("[DATE] " + ex.getMessage());
    }
    return null;
  }
  
  public static String convertToShowTime(String dateStr)
  {
    if (StringUtil.isEmptyStrnull(dateStr)) {
      return "";
    }
    String ret = dateStr;
    try
    {
      TIGHT_FORM_DATE.setLenient(false);
      java.util.Date date = TIGHT_FORM_DATE.parse(dateStr);
      
      SHOW_DATE.setLenient(false);
      ret = SHOW_DATE.format(date);
    }
    catch (ParseException e)
    {
      logger.warning("[DATE] " + e.getMessage());
    }
    return ret;
  }
  
  public static String convertToShowTime(java.util.Date date)
  {
    if (date == null) {
      return "";
    }
    String ret = "";
    try
    {
      SHOW_DATE.setLenient(false);
      ret = SHOW_DATE.format(date);
    }
    catch (Exception e)
    {
      logger.warning("[DATE] " + e.getMessage());
    }
    return ret;
  }
  
  public static String date2str(java.util.Date date, String pattern)
  {
    if ((date == null) || (StringUtil.isEmpty(pattern))) {
      return null;
    }
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat(pattern);
      return sdf.format(date);
    }
    catch (Exception ex)
    {
      logger.warning("[DATE] " + ex.getMessage());
    }
    return null;
  }
  
  public static String convert(java.util.Date date)
  {
    if (date == null) {
      return null;
    }
    try
    {
      return TIGHT_FORM_DATE.format(date);
    }
    catch (Exception ex) {}
    return null;
  }
  
  public static String dateToString(Object date)
  {
    if (date == null) {
      return "";
    }
    try
    {
      date = (java.util.Date)date;
      return SIMPLE_FORM_DATE2.format(date);
    }
    catch (Exception e)
    {
      logger.warning("[DATE]" + e.getMessage());
    }
    return "";
  }
  
  public static String formatDateTime(java.util.Date date, String pattern)
  {
    if (date == null) {
      return null;
    }
    try
    {
      SimpleDateFormat fm = new SimpleDateFormat(pattern);
      return fm.format(date);
    }
    catch (Exception e)
    {
      logger.warning("[DATE]" + e.getMessage());
    }
    return null;
  }
  
  public static String get(int formatType, java.util.Date date)
  {
    if (null == date) {
      return null;
    }
    SimpleDateFormat sdf = null;
    switch (formatType)
    {
    case 1: 
      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      break;
    case 2: 
      sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      break;
    case 3: 
      sdf = new SimpleDateFormat("yyyy��MM��dd�� HH:mm:ss");
      break;
    case 4: 
      sdf = new SimpleDateFormat("yyyy��MM��dd�� HH��mm��ss��");
      break;
    case 5: 
      sdf = new SimpleDateFormat("yyyy��MM��dd�� E HH:mm:ss");
      break;
    case 6: 
      sdf = new SimpleDateFormat("yyyy��MM��dd�� E HH��mm��ss��");
      break;
    case 7: 
      sdf = new SimpleDateFormat("yyyyMMdd");
      break;
    case 8: 
      sdf = new SimpleDateFormat("yyyyMMddHHmmss");
      break;
    case 9: 
      sdf = new SimpleDateFormat("yyyy-MM-dd");
      break;
    case 10: 
      sdf = new SimpleDateFormat("yyyy_MM");
      break;
    case 11: 
      sdf = new SimpleDateFormat("yyyy");
      break;
    case 12: 
      sdf = new SimpleDateFormat("yyyyMM");
      break;
    case 13: 
      sdf = new SimpleDateFormat("yyyy-MM");
      break;
    default: 
      sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    }
    return sdf.format(date);
  }
  
  public static java.util.Date stringToDate(String strDate)
  {
    if ((strDate != null) && (!"".equals(strDate)))
    {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
      formatter.setLenient(false);
      ParsePosition pos = new ParsePosition(0);
      java.util.Date strtodate = formatter.parse(strDate, pos);
      return strtodate;
    }
    return null;
  }
  
  public static java.util.Date convertStr2DateForQuery(String source)
    throws ParseException
  {
    if ((source == null) || (source.trim().length() != 14)) {
      return null;
    }
    java.util.Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setLenient(false);
    date = sdf.parse(source);
    
    return date;
  }
  
  public static java.util.Date convertStr2Date(String source)
    throws ParseException
  {
    if ((source == null) || (source.trim().length() != 14)) {
      return null;
    }
    java.util.Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setLenient(false);
    date = sdf.parse(source);
    
    long ltemp = date.getTime() - new java.util.Date().getTime();
    if ((ltemp > 43200000L) || (ltemp < -43200000L)) {
      return null;
    }
    return date;
  }
  
  public static String date2Str(java.util.Date date)
  {
    return date2Str(date, "yyyy-MM-dd HH:mm:ss");
  }
  
  public static String date2Str(java.util.Date date, String format)
  {
    if ((date != null) && (!StringUtil.isEmptyStrnull(format)))
    {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      return sdf.format(date);
    }
    return "";
  }
  
  public static String formatDate2Str(java.util.Date date)
  {
    if (date == null) {
      return "";
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setLenient(false);
    return sdf.format(date);
  }
  
  public static java.util.Date str2Date(String date)
  {
    if (StringUtil.notEmpty(date))
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      sdf.setLenient(false);
      try
      {
        return sdf.parse(date);
      }
      catch (ParseException e)
      {
        return new java.util.Date();
      }
    }
    return null;
  }
  
  public static java.util.Date str2Date(String date, String format)
  {
    if (StringUtil.notEmpty(date))
    {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      try
      {
        return sdf.parse(date);
      }
      catch (ParseException e)
      {
        return new java.util.Date();
      }
    }
    return null;
  }
  
  public static java.util.Date convert(String date)
  {
    try
    {
      return TIGHT_FORM_DATE.parse(date);
    }
    catch (ParseException e)
    {
      try
      {
        return SIMPLE_FORM_DATE.parse(date);
      }
      catch (ParseException e1)
      {
        throw new IllegalArgumentException("��������������, " + date);
      }
    }
  }
  
  public static String strConvertDateStr(String strDate, String format1, String format2)
  {
    if ((strDate == null) || ("".equals(strDate))) {
      return strDate;
    }
    DateFormat df1 = new SimpleDateFormat(format1);
    DateFormat df2 = new SimpleDateFormat(format2);
    df1.setLenient(false);
    df2.setLenient(false);
    String resultDate = "";
    try
    {
      resultDate = df2.format(df1.parse(strDate));
    }
    catch (Exception e) {}
    return resultDate;
  }
  
  public static String nDayDate(int n, String format1)
  {
    Calendar cal = Calendar.getInstance();
    cal.add(5, n);
    
    SimpleDateFormat format = new SimpleDateFormat(format1);
    format.setLenient(false);
    
    return format.format(cal.getTime());
  }
  
  public static String formatDate(String day, int formatType)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try
    {
      java.util.Date date = sdf.parse(day);
      if (formatType == 1) {
        sdf = new SimpleDateFormat("yyyy��MM��dd��");
      }
      if (formatType == 2) {
        sdf = new SimpleDateFormat("yyyy��MM��dd�� HH��mm��ss��");
      }
      return sdf.format(date);
    }
    catch (ParseException e) {}
    return null;
  }
  
  public static Map<String, String> dateStrToStr(String dateString)
  {
    if (dateString != null)
    {
      StringBuffer stringBuffer = new StringBuffer();
      stringBuffer.append(dateString.substring(0, 4));
      stringBuffer.append("-");
      stringBuffer.append(dateString.substring(4, 6));
      stringBuffer.append("-");
      stringBuffer.append(dateString.substring(6, 8));
      Map<String, String> map = new HashMap();
      map.put("beginTime", stringBuffer + " 00:00:00:00");
      map.put("endTime", stringBuffer + " 23:59:59:58");
      return map;
    }
    return null;
  }
  
  public static java.util.Date strToDate(String dateString)
  {
    String lineOneString = dateString.substring(0, dateString.length());
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(lineOneString.substring(0, 4));
    stringBuffer.append("-");
    stringBuffer.append(lineOneString.substring(4, 6));
    stringBuffer.append("-");
    stringBuffer.append(lineOneString.substring(6, 8));
    stringBuffer.append(" ");
    stringBuffer.append(dateString.substring(8, 10));
    stringBuffer.append(":");
    stringBuffer.append(dateString.substring(10, 12));
    stringBuffer.append(":");
    stringBuffer.append(dateString.substring(12, 14));
    return str2Date(stringBuffer.toString());
  }
  
  public static java.util.Date strToDate(String dateString, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    try
    {
      return sdf.parse(dateString);
    }
    catch (ParseException e) {}
    return null;
  }
  
  public static String convertStrTimestamp(String str)
  {
    if (StringUtil.isEmpty(str)) {
      return "";
    }
    String timeStamp = str.replace("-", "");
    timeStamp = timeStamp.replace(":", "");
    timeStamp = timeStamp.replace(" ", "");
    return timeStamp;
  }
  
  public static String TimestampToStr(String str)
  {
    if (StringUtil.isEmpty(str)) {
      return "";
    }
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(str.substring(0, 4));
    stringBuffer.append("-");
    stringBuffer.append(str.substring(4, 6));
    stringBuffer.append("-");
    stringBuffer.append(str.substring(6, 8));
    return stringBuffer.toString();
  }
  
  public static Long changeDateToLong(String dateString, String dateFormt)
  {
    if ((StringUtil.isEmpty(dateString)) || (StringUtil.isEmpty(dateFormt))) {
      return null;
    }
    SimpleDateFormat df = new SimpleDateFormat(dateFormt);
    java.util.Date date = new java.util.Date();
    try
    {
      date = df.parse(dateString);
    }
    catch (ParseException e) {}
    Long dateLong = Long.valueOf(date.getTime());
    return Long.valueOf(dateLong.longValue() / 1000L);
  }
  
  public static String getMonthOne(java.util.Date currentTime)
  {
    if (currentTime == null) {
      return "";
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(currentTime);
    calendar.add(2, -1);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    String mDateTime = formatter.format(calendar.getTime());
    return mDateTime;
  }
  
  public static String getOneDay(java.util.Date currentTime)
  {
    if (currentTime == null) {
      return "";
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(currentTime);
    calendar.add(5, -1);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    String mDateTime = formatter.format(calendar.getTime());
    return mDateTime;
  }
  
  public static String getDate(java.util.Date date, String dateString)
    throws ParseException
  {
    SimpleDateFormat sd = new SimpleDateFormat(dateString);
    sd.setLenient(false);
    String to_date = sd.format(date);
    return to_date;
  }
  
  public static java.util.Date getDate(String strDate)
    throws ParseException
  {
    DateFormat format = null;
    if (strDate.contains("-")) {
      format = new SimpleDateFormat("yyyy-MM-dd");
    } else {
      format = new SimpleDateFormat("yyyyMMddHHmmss");
    }
    java.util.Date date = null;
    try
    {
      format.setLenient(false);
      date = format.parse(strDate);
    }
    catch (Exception e)
    {
      logger.info(e.getMessage());
    }
    return date;
  }
  
  public static java.util.Date convert2Date(String strDate, String dateString)
    throws ParseException
  {
    if ((StringUtil.isEmpty(strDate)) || (StringUtil.isEmpty(dateString))) {
      return null;
    }
    DateFormat format = new SimpleDateFormat(dateString);
    java.util.Date date = null;
    format.setLenient(false);
    date = format.parse(strDate);
    return date;
  }
  
  public static java.util.Date convertToDate(String strDate, String dateString)
  {
    if ((StringUtil.isEmpty(strDate)) || (StringUtil.isEmpty(dateString))) {
      return null;
    }
    DateFormat format = new SimpleDateFormat(dateString);
    java.util.Date date = null;
    format.setLenient(false);
    try
    {
      date = format.parse(strDate);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
    return date;
  }
  
  public static String getTimestamp(String strDate)
  {
    if (StringUtil.notEmpty(strDate)) {
      return strDate.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "").trim();
    }
    return "";
  }
  
  public static java.util.Date addDays(java.util.Date date, int ndays)
  {
    if (date == null) {
      return null;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(6, ndays);
    
    return calendar.getTime();
  }
  
  public static java.util.Date calStringToDate(Object date)
  {
    String dateStr = StringUtil.toStringAndTrim(date);
    if (StringUtil.isEmpty(dateStr)) {
      return null;
    }
    dateStr = dateStr.replaceAll("-", "");
    
    return new java.util.Date(bocmDateToCal(dateStr).getTimeInMillis());
  }
  
  public static String rolDate(String inputDate, long days)
  {
    java.util.Date inday = bocmDateToCal(inputDate).getTime();
    long l = inday.getTime();
    long rol = l - days * 24L * 60L * 60L * 1000L;
    java.util.Date rolDay = new java.util.Date(rol);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(rolDay);
    int i = calendar.get(1);
    int j = calendar.get(2) + 1;
    int k = calendar.get(5);
    return "" + i + (j >= 10 ? "" + j : new StringBuilder().append("0").append(j).toString()) + (k >= 10 ? "" + k : new StringBuilder().append("0").append(k).toString());
  }
  
  public static java.util.Date rolDate(java.util.Date inputDate, long days)
  {
    SimpleDateFormat DATE_FORMATE = new SimpleDateFormat("yyyyMMdd");
    String ret = rolDate(DATE_FORMATE.format(inputDate), days);
    return new java.util.Date(bocmDateToCal(ret).getTimeInMillis());
  }
  
  public static Calendar bocmDateToCal(String s)
  {
    int i = Integer.parseInt(s.substring(0, 4));
    int j = Integer.parseInt(s.substring(4, 6)) - 1;
    int k = Integer.parseInt(s.substring(6, 8));
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(i, j, k, 0, 0, 0);
    return calendar;
  }
  
  private static java.util.Date alterDate(java.util.Date date, int field, int amount)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(field, amount);
    return calendar.getTime();
  }
  
  public static java.util.Date alterYear(java.util.Date date, int amount)
  {
    return alterDate(date, 1, amount);
  }
  
  public static java.util.Date alterMonty(java.util.Date date, int amount)
  {
    return alterDate(date, 2, amount);
  }
  
  public static java.util.Date alterDay(java.util.Date date, int amount)
  {
    return alterDate(date, 5, amount);
  }
  
  public static java.util.Date alterHour(java.util.Date date, int amount)
  {
    return alterDate(date, 11, amount);
  }
  
  public static java.util.Date alterMinute(java.util.Date date, int amount)
  {
    return alterDate(date, 12, amount);
  }
  
  public static java.util.Date alterSecond(java.util.Date date, int amount)
  {
    return alterDate(date, 13, amount);
  }
  
  public static java.util.Date getStartTime(String date)
    throws ParseException
  {
    if (StringUtil.isEmpty(date)) {
      return null;
    }
    String start = date + "000000";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setLenient(false);
    return sdf.parse(start);
  }
  
  public static java.util.Date getStartTimeOfDay()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new java.util.Date());
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    java.util.Date date = calendar.getTime();
    return date;
  }
  
  public static java.util.Date convert2StartDate(java.util.Date source)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(source);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    return calendar.getTime();
  }
  
  public static java.util.Date convert2EndDate(java.util.Date source)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(source);
    calendar.set(11, 23);
    calendar.set(12, 59);
    calendar.set(13, 59);
    return calendar.getTime();
  }
  
  public static String getPolicyStartTime(java.util.Date source)
  {
    return get(1, source);
  }
  
  public static String getPolicyEndTime(java.util.Date source)
  {
    return get(9, source) + " 24:00:00";
  }
  
  public static String getTransferedPolicyStartTime(java.util.Date source)
  {
    return get(8, convert2StartDate(source));
  }
  
  public static String getTransferedPolicyEndTime(java.util.Date source)
  {
    return get(8, alterSecond(source, 1));
  }
  
  public static String getStartTime(java.util.Date source)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(source);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    java.util.Date date = calendar.getTime();
    return get(1, date);
  }
  
  public static String getStartTime(int format, java.util.Date source)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(source);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    java.util.Date date = calendar.getTime();
    return get(format, date);
  }
  
  public static String getEndTime(java.util.Date source)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(source);
    calendar.set(11, 23);
    calendar.set(12, 59);
    calendar.set(13, 59);
    java.util.Date date = calendar.getTime();
    return get(1, date);
  }
  
  public static java.util.Date getEndTime(String date)
    throws ParseException
  {
    String end = date + "235959";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setLenient(false);
    return sdf.parse(end);
  }
  
  public static java.sql.Date getCurrDate()
  {
    return new java.sql.Date(System.currentTimeMillis());
  }
  
  public static java.util.Date getTodayZeroDate()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);
    return calendar.getTime();
  }
  
  public static String getCurrentDateTime()
  {
    SimpleDateFormat DATE_FORMATE = new SimpleDateFormat("yyyyMMddHHmmss");
    return DATE_FORMATE.format(new java.util.Date(System.currentTimeMillis()));
  }
  
  public static String getYear()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split("-")[0];
  }
  
  public static String getMonth()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split("-")[1];
  }
  
  public static String getDate()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split("-")[2].split(" ")[0];
  }
  
  public static String getCurrentDate()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split(" ")[0];
  }
  
  public static String getCurrentTime()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split(" ")[1];
  }
  
  public static String getCurrentFullTime()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date());
  }
  
  public static String getHours()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split(" ")[1].split(":")[0];
  }
  
  public static String getMinutes()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split(" ")[1].split(":")[1];
  }
  
  public static String getSeconds()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    return sdf.format(new java.util.Date()).split(" ")[1].split(":")[2];
  }
  
  public static java.util.Date stringToDate(String str, String format)
  {
    if ((StringUtil.isEmpty1(str)) || (StringUtil.isEmpty1(format))) {
      return null;
    }
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      return sdf.parse(str);
    }
    catch (ParseException e) {}
    return null;
  }
  
  public static java.util.Date stringToDate1(String str)
  {
    if (StringUtil.isEmpty1(str)) {
      return null;
    }
    return stringToDate(str, "yyyy-MM-dd");
  }
  
  public static java.util.Date stringToDateForQuery(String str)
  {
    if ((StringUtil.isEmpty1(str)) || (str.length() != 14)) {
      return null;
    }
    return stringToDate(str, "yyyyMMddHHmmss");
  }
  
  public static java.util.Date stringToDateCompareNow(String str, long time)
  {
    if ((StringUtil.isEmpty1(str)) || (time < 0L)) {
      return null;
    }
    java.util.Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setLenient(false);
    try
    {
      date = sdf.parse(str);
    }
    catch (ParseException e)
    {
      return null;
    }
    long timediff = date.getTime() - new java.util.Date().getTime();
    if ((timediff > time) || (timediff < -time)) {
      return null;
    }
    return date;
  }
  
  public static java.util.Date stringToDateCompareNow(String str)
  {
    if (StringUtil.isEmpty1(str)) {
      return null;
    }
    return stringToDateCompareNow(str, 43200000L);
  }
  
  public static java.util.Date stringToDate(int formattype, String str)
  {
    if (StringUtil.isEmpty1(str)) {
      return null;
    }
    String type = "";
    switch (formattype)
    {
    case 1: 
      type = "yyyy-MM-dd HH:mm:ss";
      break;
    case 2: 
      type = "yyyy/MM/dd HH:mm:ss";
      break;
    case 3: 
      type = "yyyy��MM��dd�� HH:mm:ss";
      break;
    case 4: 
      type = "yyyy��MM��dd�� HH��mm��ss��";
      break;
    case 5: 
      type = "yyyy��MM��dd�� E HH:mm:ss";
      break;
    case 6: 
      type = "yyyy��MM��dd�� E HH��mm��ss��";
      break;
    case 7: 
      type = "yyyyMMdd";
      break;
    case 8: 
      type = "yyyyMMddHHmmss";
      break;
    case 9: 
      type = "yyyy-MM-dd";
      break;
    case 10: 
      type = "yyyy_MM";
      break;
    case 11: 
      type = "yyyy";
      break;
    case 12: 
      type = "yyyyMM";
      break;
    case 13: 
      type = "yyyy-MM";
      break;
    default: 
      type = "yyyyMMddHHmmss";
    }
    return stringToDate(str, type);
  }
  
  public static String stringToTimestamp(String strtime)
  {
    if (StringUtil.isEmpty1(strtime)) {
      return "";
    }
    return strtime.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
  }
  
  public static String dateToString(java.util.Date date, String format)
  {
    if ((date == null) || (StringUtil.isEmpty1(format))) {
      return "";
    }
    String retstr = "";
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      retstr = sdf.format(date);
    }
    catch (Exception e)
    {
      return "";
    }
    return retstr;
  }
  
  public static String dateToString(java.util.Date date)
  {
    if (date == null) {
      return "";
    }
    return dateToString(date, "yyyyMMddHHmmss");
  }
  
  public static String dateToString(Object obj, String format)
  {
    if ((StringUtil.isEmpyt1(obj)) || (StringUtil.isEmpty1(format))) {
      return "";
    }
    String retstr = "";
    try
    {
      java.util.Date date = (java.util.Date)obj;
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      retstr = sdf.format(date);
    }
    catch (Exception e)
    {
      return "";
    }
    return retstr;
  }
  
  public static String dateToString1(Object obj)
  {
    if (StringUtil.isEmpyt1(obj)) {
      return "";
    }
    return dateToString(obj, "yyyyMMdd");
  }
  
  public static Long dateToSeconds(String datestr, String format)
  {
    if ((StringUtil.isEmpty1(datestr)) || (StringUtil.isEmpty1(format))) {
      return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    java.util.Date date = null;
    try
    {
      date = sdf.parse(datestr);
    }
    catch (ParseException e)
    {
      return null;
    }
    if (date != null)
    {
      Long ret = Long.valueOf(date.getTime());
      return Long.valueOf(ret.longValue() / 1000L);
    }
    return Long.valueOf(0L);
  }
  
  public static String timestampToString(String timestamp)
  {
    if ((StringUtil.isEmpty1(timestamp)) || (!StringUtil.isNumeric(timestamp))) {
      return "";
    }
    String str = timestamp.replaceAll(" ", "");
    StringBuffer stringBuffer = new StringBuffer();
    int length = str.length();
    if (length == 8)
    {
      stringBuffer.append(str.substring(0, 4));
      stringBuffer.append("-");
      stringBuffer.append(str.substring(4, 6));
      stringBuffer.append("-");
      stringBuffer.append(str.substring(6, 8));
      return stringBuffer.toString();
    }
    if (length == 14)
    {
      stringBuffer.append(str.substring(0, 4));
      stringBuffer.append("-");
      stringBuffer.append(str.substring(4, 6));
      stringBuffer.append("-");
      stringBuffer.append(str.substring(6, 8));
      stringBuffer.append(" ");
      stringBuffer.append(str.substring(8, 10));
      stringBuffer.append(":");
      stringBuffer.append(str.substring(10, 12));
      stringBuffer.append(":");
      stringBuffer.append(str.substring(12, 14));
      return stringBuffer.toString();
    }
    return "";
  }
  
  public static String convertToShowTime1(String str, String format)
  {
    if ((StringUtil.isEmpty1(str)) || (StringUtil.isEmpty1(format))) {
      return "";
    }
    String retstr = "";
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
      sdf.setLenient(false);
      java.util.Date date = sdf.parse(str);
      sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      retstr = sdf.format(date);
    }
    catch (Exception e)
    {
      return "";
    }
    return retstr;
  }
  
  public static String convertToShowTime1(String str)
  {
    if (StringUtil.isEmpty1(str)) {
      return "";
    }
    return convertToShowTime1(str, "yyyy��MM��dd�� HH:mm");
  }
  
  public static String convertToShowTime1(java.util.Date date, String format)
  {
    if ((date == null) || (StringUtil.isEmpty1(format))) {
      return "";
    }
    String retstr = "";
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      retstr = sdf.format(date);
    }
    catch (Exception e)
    {
      return "";
    }
    return retstr;
  }
  
  public static String convertToShowTime1(java.util.Date date)
  {
    if (date == null) {
      return "";
    }
    return convertToShowTime1(date, "yyyy��MM��dd�� HH:mm");
  }
  
  public static Map<String, String> dateStrToMap(String date, String beginTime, String endTime)
  {
    if ((StringUtil.isEmpty1(date)) || (date.length() < 8)) {
      return null;
    }
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(date.substring(0, 4));
    stringBuffer.append("-");
    stringBuffer.append(date.substring(4, 6));
    stringBuffer.append("-");
    stringBuffer.append(date.substring(6, 8));
    Map<String, String> map = new HashMap();
    map.put(beginTime, stringBuffer.toString().trim() + " 00:00:00:00");
    map.put(endTime, stringBuffer.toString().trim() + " 23:59:59:58");
    return map;
  }
  
  public static int getDiffMonth(java.util.Date beginDate, java.util.Date endDate)
  {
    Calendar calbegin = Calendar.getInstance();
    Calendar calend = Calendar.getInstance();
    calbegin.setTime(beginDate);
    calend.setTime(endDate);
    int m_begin = calbegin.get(2) + 1;
    int m_end = calend.get(2) + 1;
    
    int checkmonth = m_end - m_begin + (calend.get(1) - calbegin.get(1)) * 12;
    return checkmonth;
  }
  
  public static int getDiffDay(java.util.Date beginDate, java.util.Date endDate)
  {
    int days = (int)((endDate.getTime() - beginDate.getTime()) / 86400000L);
    if (days <= 0) {
      return 1;
    }
    return days;
  }
  
  public static int getDiffYear(java.util.Date beginDate, java.util.Date endDate)
  {
    Calendar cal = Calendar.getInstance();
    cal.setTime(beginDate);
    if (cal.before(endDate)) {
      return 0;
    }
    int yearNow = cal.get(1);
    int monthNow = cal.get(2);
    int dayOfMonthNow = cal.get(5);
    cal.setTime(endDate);
    
    int yearOfEndDate = cal.get(1);
    int monthOfEndDate = cal.get(2);
    int dayOfEndDate = cal.get(5);
    
    int diffYear = yearNow - yearOfEndDate;
    if (monthNow <= monthOfEndDate) {
      if (monthNow == monthOfEndDate)
      {
        if (dayOfMonthNow < dayOfEndDate) {
          diffYear--;
        }
      }
      else {
        diffYear--;
      }
    }
    return diffYear;
  }
  
  public static int getAge(java.util.Date birthday)
  {
    return getDiffYear(new java.util.Date(), birthday);
  }
  
  public static int getDiffMins(java.util.Date beginDate, java.util.Date endDate)
  {
    return (int)((endDate.getTime() - beginDate.getTime()) / 60000L);
  }
  
  public static boolean isOneDay(java.util.Date one, java.util.Date other)
  {
    return get(7, one).equals(get(7, other));
  }
  
  public static boolean isSameTime(java.util.Date one, java.util.Date other)
  {
    return get(1, one).equals(get(1, other));
  }
  
  public static java.util.Date compactInsuranceEndDate(java.util.Date beginDate, java.util.Date endDate)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(endDate);
    int endDay = calendar.get(5);
    
    Calendar calendar0 = Calendar.getInstance();
    calendar0.setTime(beginDate);
    int startDay = calendar0.get(5);
    if (startDay <= endDay) {
      return alterDay(endDate, -1);
    }
    return endDate;
  }
  
  public static boolean isSameDay(java.util.Date date1, java.util.Date date2)
  {
    if ((date1 == null) || (date2 == null)) {
      throw new IllegalArgumentException("The date must not be null");
    }
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isSameDay(cal1, cal2);
  }
  
  public static boolean isSameDay(Calendar cal1, Calendar cal2)
  {
    if ((cal1 == null) || (cal2 == null)) {
      throw new IllegalArgumentException("The date must not be null");
    }
    return (cal1.get(0) == cal2.get(0)) && (cal1.get(1) == cal2.get(1)) && (cal1.get(6) == cal2.get(6));
  }
}

